import config from "AppConfig/index";

function getKey(key) {
    return `${config.envVersion}_${key}`
}

const storage = {
    set(key, value, expires = 1000 * 60 * 60 * 24 * 365) {
        wx.setStorageSync(getKey(key), { value, expires: Date.now() + expires })
    },
    get(key) {
        const data = wx.getStorageSync(getKey(key))
        if (!data) {
            return null
        }
        const now = Date.now()
        if (data.expires < now) {
            this.remove(key)
            return null
        }
        return data.value
    },
    remove(key) {
        wx.removeStorageSync(getKey(key))
    }
}

export default storage